package _4Game;

import java.net.Socket;
import java.util.HashMap;
import java.util.Map;

public class SnakeGame {
	
	GameServerFrame f;
	
	GameServer gs;

	// 定义一个容器存放所有蛇的信息
	Map<Socket,Snake> map = new HashMap<>();
	
	// 刷新间隔 500ms
	final static int TIME_SPAN = 2000;
	
	// 放入一条蛇
	public void addSnake(Socket s)
	{
		// 创建回一条蛇
		Snake snake = new Snake();
		snake.body.add(new Point(5,5));
		snake.body.add(new Point(5,6));
		snake.body.add(new Point(5,7));
		snake.direction = 3;
		
		// 放入map
		map.put(s, snake);
	}
	
	public void start()
	{
		
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				
				while(true)
				{
					try {
						Thread.sleep(TIME_SPAN);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					
					// 更新蛇的位置信息
					for(Snake s:map.values())
					{
						// 更新蛇的往前移动一格
						s.move();
						System.out.println(s);
					}
					
					
					// 通过发送给客户端
					gs.sendMap(map);
					
//					System.out.println("xxx数量:"+map.size());
					f.showSanke(map);
					
				}
				
			}
		}).start();
	}
	
	
}
